<!DOCTYPE html>

<?php
    require_once("core/functions.php"); //includes the core functions of the system's

    if (session_status() == PHP_SESSION_NONE) {
        session_start();
    }
    $page = null;
    $guestMessage = null;
    if (isset($_SESSION["personId"])) {
        $personId = $_SESSION["personId"];
        $page = new vwallController($personId);
        $loggedIn = true;
        $accountType = $page->getAccountType();
        $activated = $page->personActivated();
        $hasAccess = $page->hasAccess();
    } else {
        $loggedIn = false;
    }

    if (isset($_REQUEST["lEmail"]) && isset($_REQUEST["lPassword"])) {
        $logEmail = $_REQUEST["lEmail"];
        $logPass = $_REQUEST["lPassword"];
        $logController = new LoginController($logEmail, $logPass);
        $result = $logController->tryLogin();
        if ($result == LOGIN_SUCCESS) {
            $_SESSION['personId'] = $logController->getId();
            $accType = $logController->getAccountType();
            if ($accType == ACC_ADMIN) {
                core::redirect("managementpanel.php");
            } else {
                core::redirect("index.php");
            }
        } else {
            $loginerror = "ERROR : " . LOGIN_ERROR;
        }
    }

    if (isset($_POST["mailingListEmail"])) {
        $email = core::sanitize($_POST["mailingListEmail"]);
        if (core::addToMailingList($email)) {
            $guestMessage = $email . " has been addded to Mailing List";
        } else {
            $guestMessage = $email . " has been failed to be added to mailing list";
        }
    }

?>
<html>
    <head>
        <title>
<?php
    if (isset($title)) {
        echo $title;
    } else {
        echo "Welcome";
    }
?>
        </title>
        <link rel="stylesheet" href="stylesheet/ui.css" media="screen"/>        
        <link rel="stylesheet" href="stylesheet/theme.css" media="screen"/>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="shortcut icon" href="images/favicon.ico" type="image/vnd.microsoft.icon" />
        <script type="text/javascript" src="javascript/UTSDK.js"></script>
        <script type="text/javascript">
            var search;
            window.addEventListener("load", function() {
                var login = UTSDK.$("login");
                var loginTab = UTSDK.$("loginTab");
                if (login && loginTab) {
                    var loginContainer = new UTSDK.popup(login, loginTab);
                    loginTab.addEventListener("click", function(evt) {
                        loginContainer.toggle(evt);
                    });
                }
                var profile = UTSDK.$("profile");
                var profileTab = UTSDK.$("profileTab");
                if (profile && profileTab) {
                    var profileContainer = new UTSDK.popup(profile, profileTab);
                    profileTab.addEventListener("click", function(evt) {
                        profileContainer.toggle(evt);
                    });
                }
                var searchInput = UTSDK.$("searchInput");
                if (searchInput) {
                    search = new UTSDK.search(searchInput);
                }

                var error = UTSDK.$("error");
                for (var i = 0; i < error.children.length; i++) {
                    if (error.children[i].textContent.replace(/^\s*/, "").replace(/\s*$/, "") !== "")
                        error.children[i].style.display = "block";
                }
            });
            
            function resendActivation() {
                var json = {
                    resendActivation: <?= ((isset($personId)) ? $personId : 0) ?>
                };
                UTSDK.$("loading").innerHTML = '<img src="images/ajax-loader.gif"/>';
                UTSDK.AJAX.get("update.php", json, function(){
                    var p = UTSDK.$("sendEmail");
                    p.innerHTML = "The email has be resent, ensure you check your spam folder as well";
                    UTSDK.$("loading").innerHTML = '';
                }, true);
            }
        </script>
    </head>
    <body>
        <div id="login" class="hidden">
            <form METHOD="POST" ACTION="<?= $_SERVER['PHP_SELF']; ?>" onsubmit="return validateLogForm();">
                <input id="loginEmail" type="text" name="lEmail" placeholder="email@domain.com"/><br/>
                <input id="loginPassword" type="password" name="lPassword" placeholder="password"/>
                <input type="submit" value="Login" id="loginsubmit" /><br/>
                <a href="register.php">Register</a>
            </form>
        </div>
        <div id="profile" class="hidden">
            <label>Welcome,
<?php
    if ($loggedIn) {
        $page->printName();
    }
?>  
            </label><br/><br/>
            <input type="button" value="Edit Profile" onclick="UTSDK.redirect('editprofile.php');"/>
            <input type="button" value="Logout" onclick="UTSDK.redirect('logout.php');"/>
                <?php if ($loggedIn) { if( $page->hasAccess()){ ?>
                    <br/>
                    <input class="managementBtn" type="button" value="Management Panel" 
                           onclick="UTSDK.redirect('managementpanel.php')"/>
                <?php } } ?>
            <br/><br/>
        </div>
        <div class="header">
            <div class="headerContent">
                <div class="logo">
                    <a title="Home" href="http://www.uts.edu.au" class="logoLink">
                        <img alt="Home" class="logo" src="images/logo_2.png"/>
                    </a>
                </div>
                <div class="tab" onclick="UTSDK.redirect('index.php');">Home</div>
                <div class="tab" onclick="UTSDK.redirect('timetable.php');">Timetable</div>
                <div class="tab" onclick="UTSDK.redirect('viewEvent.php');">Event</div>
                <?php if ($loggedIn) { ?>
                <div class="tab" id="profileTab">Profile</div>
                <?php } else { ?>
                <div class="tab" id="loginTab">Login</div>
                <?php } ?>
            </div>
        </div>
        <div id="search" class="searchDiv">
            <div id="searchBar">
                <input id="searchInput" class="searchInput" type="text" placeholder="Search..."/><!--
                --><input type="submit" class="searchBtn" 
                          value="Search for event" onclick="search.submit()"/>
            </div>
        </div>
        <div id="error">
            <label class="pageError" style="display:none">
                <?=($loggedIn ? $page->printMessages() : false ) ?>
                <?= (isset($error) ? $error : false )?>
            </label>
            <label class="pageHeaderMessage" style="display:none">
            <?php
                if (!is_null($guestMessage)) {
                    echo $guestMessage;
                    $guestMessage = null;
                }
            ?>
            </label>
        </div>
        <div class="mainBody">
            <div class="centerContent">
                <?php
                    if ($loggedIn) {
                        if (!$page->personActivated()) {
                            echo "<h1>Activate</h1>";
                            echo "<p id='sendEmail'>The activation key was emailed to the address you entered when you registered!</p>";
                            echo '<form method="GET" action="activate.php">';
                            echo '<input type="hidden" name="personId" value="' . $personId . '"/>';
                            echo '<input type="text" name="activationKey" placeholder="activation key"/> <input type="submit" value="activate"/>';
                            echo '<br/><br/><label class="link" onclick="resendActivation()">Resend activation code</label>   <div style="display:inline" id="loading"></div>';
                            echo '</form>';
                            die();
                        }
                    }
                ?>
            </div>